using System;
using System.Collections.Generic;
using System.Linq;
using HCP.SimpleJSON;
using UnityEngine;

namespace HCP
{
	public abstract class JobRequest
	{
		public JobRequest(JSONClass json)
		{
			if (json == null)
			{
				throw new ArgumentNullException("JobRequest cannot assign null data");
			}
			this.m_data = json;
		}

		protected JSONNode Data
		{
			get
			{
				return this.m_data;
			}
		}

		public abstract JobResponse Process();

		protected static string ConstructElementId(Component c)
		{
			GameObject gameObject = c.gameObject;
			Sticky component = gameObject.GetComponent<Sticky>();
			string result;
			if (component)
			{
				result = string.Format("%0:%1", component.Id, c.GetType().Name);
			}
			else
			{
				result = c.GetInstanceID().ToString();
			}
			return result;
		}

		protected static void RegisterElement(string id, Component c)
		{
			if (JobRequest.s_touchedElements.ContainsKey(id))
			{
				JobRequest.s_touchedElements[id] = c;
			}
			else
			{
				JobRequest.s_touchedElements.Add(id, c);
			}
		}

		protected static Component GetElementById(string id)
		{
			Component component;
			if (JobRequest.s_touchedElements.ContainsKey(id))
			{
				component = JobRequest.s_touchedElements[id];
			}
			else
			{
				bool flag = ElementId.IsSticky(id);
				if (flag)
				{
					string objectPart = ElementId.ObjectPart(id);
					string typeName = ElementId.ComponentPart(id);
					Sticky sticky = Resources.FindObjectsOfTypeAll<Sticky>().First((Sticky s) => s.Id == objectPart);
					component = sticky.GetComponent(Type.GetType(typeName));
				}
				else
				{
					int intId = int.Parse(id);
					component = Resources.FindObjectsOfTypeAll<Component>().First((Component c) => c.GetInstanceID() == intId);
				}
				JobRequest.RegisterElement(id, component);
			}
			return component;
		}

		private JSONNode m_data;

		protected static Dictionary<string, Component> s_touchedElements = new Dictionary<string, Component>();
	}
}
